/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import moment from 'moment';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';

export function getNewDate(
  date: moment.MomentInput = undefined,
  format = DATE_FORMAT,
) {

  const dateString = moment(date).format(format);; // 假设您有一个这样的字符串

  // 使用split方法将字符串按短横线分隔成数组
  const dateParts = dateString.split("-");

  // 由于JavaScript的Date构造函数接受的参数顺序是年、月、日，因此可以直接从数组中提取对应部分
  const year = parseInt(dateParts[0], 10);
  const month = parseInt(dateParts[1], 10) - 1; // 注意，月份是从0开始计数的，所以需要减1
  const day = parseInt(dateParts[2], 10);

  // 使用new Date()构造函数创建日期对象
  return new Date(year, month, day);
}
export function formatToDateTime(
  date: moment.MomentInput = undefined,
  format = DATE_TIME_FORMAT,
): string {
  return moment(date).format(format);
}

export function formatToDate(date: moment.MomentInput = undefined, format = DATE_FORMAT): string {
  return moment(date).format(format);
}
export function currentDate(dateObject: any) {
  let currentDate = {
    year: 0,
    month: 0,
    day: 0,
    hour: 0,
    minute: 0,
    second: 0
  } as any;
  currentDate.year = dateObject.getFullYear();
  currentDate.month = dateObject.getMonth() + 1;
  currentDate.day = dateObject.getDate();
  currentDate.hour = dateObject.getHours();
  currentDate.minute = dateObject.getMinutes();
  currentDate.second = dateObject.getSeconds();
  currentDate.month = String(currentDate.month).length < 2 ? '0' + currentDate.month : currentDate.month;
  currentDate.day = String(currentDate.day).length < 2 ? '0' + currentDate.day : currentDate.day;
  currentDate.hour = String(currentDate.hour).length < 2 ? '0' + currentDate.hour : currentDate.hour;
  currentDate.minute = String(currentDate.minute).length < 2 ? '0' + currentDate.minute : currentDate.minute;
  currentDate.second = String(currentDate.second).length < 2 ? '0' + currentDate.second : currentDate.second;
  return currentDate;
}

export function getDateInsure(date: any) {
  // debugger
  let dateArr = date.split("-");
  let birthYear = parseInt(dateArr[0]) + 1;
  let birthMonth = dateArr[1];
  let birthDay = dateArr[2];
  let dataString = birthYear + "-" + birthMonth + "-" + birthDay;
  if (typeof (dataString) == 'string') {
    dataString = dataString.replace(/-/g, "/");
  }
  let nowdate = new Date(dataString);
  nowdate.setTime(nowdate.getTime() - 24 * 60 * 60 * 1000);
  //获取时间的年月日时分秒对象
  let dateObj = currentDate(nowdate);
  let yyyy_MM_dd = dateObj.year + '-' + dateObj.month + '-' + dateObj.day;
  return yyyy_MM_dd;
}


//比较两个日期大小 大于等于返回 true 小于返回false
export function isTimeGreater(sDate1: any, sDate2: any) {
  sDate1 = sDate1.replace(/-/g, "/");
  sDate2 = sDate2.replace(/-/g, "/");
  let sDate1Time = new Date(sDate1).getTime();
  let sDate2Time = new Date(sDate2).getTime();
  return sDate1Time >= sDate2Time;
}

export const dateUtil = moment;
